import {HttpProvider, AjaxOption} from '../../common/http'
import {Injectable} from '@angular/core'
import {API} from '../../common/api'
import 'rxjs/add/operator/shareReplay';
import {Helper} from "../../common/Helper";

@Injectable()
export class RegisterProvider {

  constructor(public http: HttpProvider,
              public helper: Helper) {
    console.log('Hello RegisterProvider Provider');
  }

  getVerityCode(options:AjaxOption) {
    return this.http.requestWithoutToken({
      type: 'POST',
      url: API.USER_SEND_CODE,
      data: options.data
    }).then(data=>{
      options.success && options.success()
    }).catch(err => {
      this.http.toastMsg(err.msg)
    })
  }

  checkPhone(options:AjaxOption) {
    return this.http.requestWithoutToken({
      type: 'GET',
      url: API.USER_CHECK_CELLPHONE,
      data: options.data
    }).then(data=>{
      options.success && options.success()
    }).catch(err=>{
      options.fail && options.fail(err)
      this.http.toastMsg(err.msg)
    })
  }

  register(options: AjaxOption) {
    this.helper.loading('请稍后..')
    return this.http.requestWithoutToken({
      type: 'POST',
      url: API.USER_REGISTER,
      data: options.data ? options.data : {}
    }).then(data => {
      this.helper.stopLoading()
      //注册成功后将手机号存入 storage 登陆界面显示
      console.log(options.data.cellphone)
      this.helper.setStorage('user/phone', options.data.cellphone)
      options.complete && options.complete()
      options.success && options.success(data)
    }).catch(err => {
      this.helper.stopLoading()
      this.http.toastMsg(err.msg)
      options.complete && options.complete()
      options.fail && options.fail(err)
    })
  }

}
